<?php
include_once("robot.php");

$simulate       = true;
$operation      = "none";
$article_name   = false;
$verbose        = false;
$section_number = false;
$commit_message = false;
$section_title  = false;
$robot_config   = "robot_config.inc.php";

function usage($msg = false) {
  print "
Usage for update_article.php :

  -h  --help                           Display this message then exit
      --name '<ARTICLENAME>'           Article to dump
      --dump                           Dump article content
      --update '<FILENAME>'            Update article with <FILENAME> content
      --alternate-config <CONFIG>      Use file config-<CONFIG>.inc.php instead of robot_config.inc.php
      --no-simulation                  Write for real modification. USE THIS WITH CARE !!!
      --verbose                        Verbose mode
      --section <NB>                   Use n-th section.
      --get-section-id <SECTIONTITLE>  Return id of the selected section

$msg
";
}

for($i = 0; $i < count($argv) ; $i++) {
  if($argv[$i] == "--help" || $argv[$i] == "-h") {
    usage(); exit(0);
  } else if($argv[$i] == "--dump") {
    $operation = "dump";
  } else if($argv[$i] == "--update") {
    $operation = "update";
    $filename = $argv[++$i];
  } else if($argv[$i] == "--get-section-id") {
    $operation = "get_section_id";
    $section_title = $argv[++$i];
  } else if($argv[$i] == "--name") {
    $article_name = $argv[++$i];
  } else if($argv[$i] == "--message") {
    $commit_message = $argv[++$i];
  } else if($argv[$i] == "--section") {
    $section_number = $argv[++$i];
  } else if($argv[$i] == "--alternate-config") {
    $robot_config = "config-".$argv[++$i].".inc.php";
  } else if($argv[$i] == "--no-simulation") {
    $simulate = false;
  } else if($argv[$i] == "--verbose") {
    $verbose = true;
  } else if($argv[$i] == "--message") {
    $commit_message = $argv[++$i];
  }
}

include_once($robot_config);
$robot->setCachePath("/tmp");
$robot->setVerbose($verbose);

if($operation == "none") {
  usage("Nothing to do => exiting"); exit();

} else if($operation == "dump") {
  if(!$article_name) {
    usage("Please set title name (--name <TITLENAME>)");
    exit(1);
  }
  $robot->updateCache($article_name, $section_number);
  print $robot->getCache($article_name, $section_number)."\n";

} else if($operation == "get_section_id") {
  if(!$section_title) {
    usage("Please set section title (--get-section-id <TITLENAME>)");
    exit(1);
  }
  $robot->updateCache($article_name, $section_number);
  $sections = $robot->getSectionId($robot->getCache($article_name, $section_number), $section_title, true);
  foreach(array_keys($sections) as $i) {
    printf("%3d : ".$sections[$i]."\n", $i);
  }

} else if($operation == "update") {
  if(strlen($filename) == 0 || ! file_exists($filename)) {
    print "File '$filename' does not exist\n";
    exit(1);
  }
  if(!$article_name) {
    print "Please set article name (--name <ARTICLENAME>)\n";
    exit(1);
  }
  if(!$commit_message) {
    print "Please set message comment (--message <MSG>)\n";
    exit(1);
  }
  $robot->setCurrentModification($commit_message);
  if(!$simulate) {
    $robot->activateWrite();
    print "Updating '$article_name' with '$filename' content\n";
    $file_content = join("", file($filename));
    if(!$file_content) {
      print "Error while trying to update '$article_name' : problem while opening '$filename'\n";
      exit(1);
    }
    $robot->updateArticle($article_name, $section_number, $file_content);
  } else {
    print "Simulation mode : no modification was made\n";
  }
}

?>
